package persentation.presentDepartmentDean;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JButton;

import dataservice.courseDataService.CourseDatabaseService;
import persentation.presentStudent.InfoPanel;
import po.coursePO.CoursePO;
import vo.courseVO.CourseVO;

public class ShowCoursePanel extends JPanel {
	private JTable table;
	private DepartmentdeanTabbedPane tabbedPane;
	private ArrayList<CourseVO> courseVOs;
	private Vector data,columnName;
	private MyModel model;	
	private String depart;
	private JButton tabCloseButton;
	private InfoPanel infoPanel;

	public ShowCoursePanel(String department) {
		setSize(900, 690);
		setLayout(null); 
		
		depart=department;
		
		columnName=new Vector();
		columnName.add("课程编号");
		columnName.add("课程名称");
		columnName.add("课程性质");
		columnName.add("学分");
		columnName.add("学时");
		columnName.add("院系");
		columnName.add("上课时间");
		columnName.add("上课地点");

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setFillsViewportHeight(true);
		JScrollPane jScrollPane = new JScrollPane(table);
		jScrollPane.setBounds(44, 10, 742, 405);
		add(jScrollPane);
		
		findAllCourse();
		
		
		JButton button = new JButton("查看详细课程信息");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(289, 446, 176, 23);
		add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow()!=-1){
					CourseVO courseVO=courseVOs.get(table.getSelectedRow());
					System.out.printf("选中的行号是%s\n",table.getSelectedRow());
					System.out.printf("查看的课程名是%s\n",courseVO.getCourseName());	
					infoPanel=new InfoPanel(courseVO);
					tabbedPane.addOne(courseVO.getCourseName()+" 查看详细信息", infoPanel);
				}
			}
		});
		JButton button_1 = new JButton("刷新");
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button_1.setBounds(548, 447, 93, 23);
		add(button_1);
		button_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				findAllCourse();
			}
		});
	}

	public void setTabbedPane(DepartmentdeanTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}
	
	public void findAllCourse(){
		try {
			CourseDatabaseService u=(CourseDatabaseService) Naming.lookup("rmi://localhost:8888/courseData");
			ArrayList<CoursePO> coursePOs=u.findCourseByDepartment(depart);
			courseVOs=new ArrayList<CourseVO>();
			data=new Vector();
			for (CoursePO c:coursePOs){
				CourseVO cVO=new CourseVO(c.getCourseId(), c.getCourseName(), 
						c.getTeacherID(), c.getTeacherName(), c.getCredit(), c.getCoursePeriod(), c.getClassroom(), c.getAssistant(), 
						c.getCourseTime(), c.getAttribute(), c.getMaxStudent(),c.getTerm(),c.getDepartmentID(), c.getBook(), c.getIntroduction());
				courseVOs.add(cVO);
				Vector v=new Vector();
				v.add(c.getCourseId());
				v.add(c.getCourseName());
				v.add(c.getAttribute());
				v.add(c.getCredit());
				v.add(c.getCoursePeriod());
				v.add(c.getDepartmentID());
				v.add(c.getCourseTime());
				v.add(c.getClassroom());
				data.add(v);
			}
			model=new MyModel(data,columnName);
			table.setModel(model);;
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
